<?php
/**
 * 控制器
 *
 * @author 孙晓晔
 * @version $Id: AdminController.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(APP_LIB . 'Controller.class.php');

class ResumeController extends Controller {

	function __construct() {
		parent::__construct();
		$this->application = 'admin';
	}

	/**
	 * 预处理
	 *
	 * @param array 　$request
	 */
	function pretreat($request) {
		$p1 = '';
		session_cache_expire(720);
		if (isset($request['sid'])) {
			session_id($request['sid']);
			session_start();
		} else {
			@session_start();
			$request['sid'] = session_id();
		}

		$p1 .= '&sid=' . $request['sid'];
		$this->assign('sid', $request['sid']);
		// parent::assign('p1', $p1);

		parent::pretreat($request);

		if ($this->isAuth($request)) {
			return true;
		}

		return false;
	}

	/**
	 * 判断是否登录，已登录，则返回false
	 * 若返回true，则不继续执行
	 */
	function isAuth($request) {			
		if (empty($_SESSION['name'])) {
			$this->assign('postLink', '?do=Admin.loginPost');
			if (isset($request['rnd'])) {
				echo '1';
			} else {
				$this->display($request, 'login');
			}
			return true;
		}

		return false;
	}

	function display($request, $resource_name, $cache_id = null, $compile_id = null) {
		parent::display($request, $resource_name, $cache_id, $compile_id);
		$this->logaction($request);
	}

	/**
	 * 操作日志
	 *
	 * @param WapControler $controller
	 * @param array $request
	 */
	function logaction($request) {
		$action= $request['do'];

		if (preg_match('/^(LogAction|LogAccess)/i', $action)) {
			return;
		}

		if (preg_match('/(\.ajax_)/i', $action)) {
			return;
		}

		if (preg_match('/(audit)$/i', $action)) {
			return;
		}

		$tpl = $request['tpl'];

		$uri = $_SERVER["PHP_SELF"];
		$ip	= $_SERVER['REMOTE_ADDR'];
		$emp = $_SESSION['id'];

		$totaltime = $this->totaltime($request);

		$db = newdb(Constant::tbl_log_action);
		$fmt = "INSERT INTO %s(uri, action, tpl, totaltime, ip, emp, createtime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, Constant::tbl_log_action, $uri, $action, $tpl, $totaltime, $ip, $emp);
		// debug($sql);
		$db->query($sql);
	}

	function totaltime($request) {
		$endtime = cur_time();
		$totaltime = $endtime - $request['starttime'];

		return number_format($totaltime, 7);
	}
}
?>
